/**************************************************************************/
/*! 
    @file     pn532_drvr.h
*/
/**************************************************************************/

#ifndef __PN532_DRV_H__
#define __PN532_DRV_H__

#include "projectconfig.h"
#include "pn532.h"

#define PN532_UART
// #define PN532_SPI

#define PN532_DEBUG(fmt, args...)             printf(fmt, ##args) 

#define PN532_RSTPD_PORT                      (2)
#define PN532_RSTPD_PIN                       (2)
#define PN532_SPI_CSPORT                      (0)
#define PN532_SPI_CSPIN                       (2)

#define PN532_NORMAL_FRAME__DATA_MAX_LEN      (254)
#define PN532_NORMAL_FRAME__OVERHEAD          (8)
#define PN532_EXTENDED_FRAME__DATA_MAX_LEN    (264)
#define PN532_EXTENDED_FRAME__OVERHEAD        (11)
#define PN532_BUFFER_LEN                      (PN532_EXTENDED_FRAME__DATA_MAX_LEN + PN532_EXTENDED_FRAME__OVERHEAD)
#define PN532_UART_BAUDRATE                   (115200)

enum
{
  PN532_COMMAND_DIAGNOSE              = 0x00,
  PN532_COMMAND_GETFIRMWAREVERSION    = 0x02,
  PN532_COMMAND_GETGENERALSTATUS      = 0x04,
  PN532_COMMAND_READREGISTER          = 0x06,
  PN532_COMMAND_WRITEREGISTER         = 0x08,
  PN532_COMMAND_READGPIO              = 0x0C,
  PN532_COMMAND_WRITEGPIO             = 0x0E,
  PN532_COMMAND_SETSERIALBAUDRATE     = 0x10,
  PN532_COMMAND_SETPARAMETERS         = 0x12,
  PN532_COMMAND_SAMCONFIGURATION      = 0x14,
  PN532_COMMAND_POWERDOWN             = 0x16,
  PN532_COMMAND_RFCONFIGURATION       = 0x32,
  PN532_COMMAND_RFREGULATIONTEST      = 0x58,
  PN532_COMMAND_INJUMPFORDEP          = 0x56,
  PN532_COMMAND_INJUMPFORPSL          = 0x46,
  PN532_COMMAND_INLISTPASSIVETARGET   = 0x4A,
  PN532_COMMAND_INATR                 = 0x50,
  PN532_COMMAND_INPSL                 = 0x4E,
  PN532_COMMAND_INDATAEXCHANGE        = 0x40,
  PN532_COMMAND_INCOMMUNICATETHRU     = 0x42,
  PN532_COMMAND_INDESELECT            = 0x44,
  PN532_COMMAND_INRELEASE             = 0x52,
  PN532_COMMAND_INSELECT              = 0x54,
  PN532_COMMAND_INAUTOPOLL            = 0x60,
  PN532_COMMAND_TGINITASTARGET        = 0x8C,
  PN532_COMMAND_TGSETGENERALBYTES     = 0x92,
  PN532_COMMAND_TGGETDATA             = 0x86,
  PN532_COMMAND_TGSETDATA             = 0x8E,
  PN532_COMMAND_TGSETMETADATA         = 0x94,
  PN532_COMMAND_TGGETINITIATORCOMMAND = 0x88,
  PN532_COMMAND_TGRESPONSETOINITIATOR = 0x90,
  PN532_COMMAND_TGGETTARGETSTATUS     = 0x8A
};

/* Application level errors generated by the PN532 chip */
enum 
{
  PN532_APPERROR_NONE                 = 0x00,
  PN532_APPERROR_TIMEOUT              = 0x01,
  PN532_APPERROR_CRCERROR             = 0x02,
  PN532_APPERROR_PARITYERROR          = 0x04,
  PN532_APPERROR_FRAMINGERROR         = 0x05,
  PN532_APPERROR_BITCOLLISION         = 0x06,
  PN532_APPERROR_INSUFFICIENTBUFFER   = 0x07,
  PN532_APPERROR_RFBUFFEROVERFLOW     = 0x09,
  PN532_APPERROR_RFFIELDTIMEOUT       = 0x0A,
  PN532_APPERROR_RFPROTOCOLERROR      = 0x0B,
  PN532_APPERROR_TEMPERROR            = 0x0D,
  PN532_APPERROR_INTERNBUFFEROVERFLOW = 0x0E,
  PN532_APPERROR_INVALIDPARAMETER     = 0x10,
  PN532_APPERROR_DEP_UNSUPPORTEDCMD   = 0x12,
  PN532_APPERROR_DEP_INVALIDOFORMAT   = 0x13,
  PN532_APPERROR_AUTHENTERR           = 0x14,
  PN532_APPERROR_UIDCCHECKERROR       = 0x23,
  PN532_APPERROR_DEP_INVALIDDEVSTATE  = 0x25,
  PN532_APPERROR_OPERATIONNOTALLOWED  = 0x26,
  PN532_APPERROR_CMDNOTACCEPTABLE     = 0x27,
  PN532_APPERROR_TARGETRELEASED       = 0x29,
  PN532_APPERROR_IDMISMATCH           = 0x2A,
  PN532_APPERROR_CARDDISAPPEARED      = 0x2B,
  PN532_APPERROR_NFCID3MISMATCH       = 0x2C,
  PN532_APPERROR_OVERCURRENTEVENT     = 0x2D,
  PN532_APPERROR_NADMISSINGINDEP      = 0x2E
};

void          pn532HWInit(void);
pn532_error_t pn532BuildFrame(byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, const size_t szData);
pn532_error_t pn532SendCommand(const byte_t * pbtData, const size_t szData);
pn532_error_t pn532ReadResponse(byte_t * pbtResponse, size_t * pszRxLen);
pn532_error_t pn532Wakeup(void);

#endif
